{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mxnet import nd\n",
    "from mxnet.gluon import nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dense(None -> 2, linear)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer = nn.Dense(2)\n",
    "layer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "layer.initialize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[-0.02524132 -0.00874885]\n",
       " [-0.06026538 -0.01308061]\n",
       " [ 0.02468396 -0.02181557]]\n",
       "<NDArray 3x2 @cpu(0)>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = nd.random.uniform(-1, 1, (3, 4))\n",
    "layer(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[-0.00873779 -0.02834515  0.05484822 -0.06206018]\n",
       " [ 0.06491279 -0.03182812 -0.01631819 -0.00312688]]\n",
       "<NDArray 2x4 @cpu(0)>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer.weight.data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Sequential(\n",
       "  (0): Conv2D(None -> 6, kernel_size=(5, 5), stride=(1, 1), Activation(relu))\n",
       "  (1): MaxPool2D(size=(2, 2), stride=(2, 2), padding=(0, 0), ceil_mode=False, global_pool=False, pool_type=max, layout=NCHW)\n",
       "  (2): Conv2D(None -> 16, kernel_size=(3, 3), stride=(1, 1), Activation(relu))\n",
       "  (3): MaxPool2D(size=(2, 2), stride=(2, 2), padding=(0, 0), ceil_mode=False, global_pool=False, pool_type=max, layout=NCHW)\n",
       "  (4): Dense(None -> 120, Activation(relu))\n",
       "  (5): Dense(None -> 84, Activation(relu))\n",
       "  (6): Dense(None -> 10, linear)\n",
       ")"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net = nn.Sequential()\n",
    "net.add(\n",
    "    nn.Conv2D(channels=6, kernel_size=5, activation='relu'),\n",
    "    nn.MaxPool2D(pool_size=2, strides=2),\n",
    "    nn.Conv2D(channels=16, kernel_size=3, activation='relu'),\n",
    "    nn.MaxPool2D(pool_size=2, strides=2),\n",
    "    nn.Dense(120, activation='relu'),\n",
    "    nn.Dense(84, activation='relu'),\n",
    "    nn.Dense(10)\n",
    ")\n",
    "net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "[[-9.62446909e-04  7.94919091e-04 -6.95670547e-04 -9.92330723e-04\n",
       "   5.44202863e-04  2.16677814e-04 -3.14829114e-04  1.25210005e-04\n",
       "   7.27561186e-04  1.33355346e-03]\n",
       " [-6.39375823e-04  5.83529007e-04 -5.34477585e-04 -6.72551570e-04\n",
       "   4.14358976e-04  9.39928941e-05  2.67426280e-04 -2.74227088e-04\n",
       "   4.81882918e-04  1.16075948e-03]\n",
       " [-6.86179672e-04  3.39763268e-04 -4.80861672e-05 -3.43146385e-04\n",
       "   1.01135287e-03 -1.01338774e-04 -5.36221662e-04 -1.27039712e-05\n",
       "   1.13757094e-03  1.15586189e-03]\n",
       " [-5.21273178e-04  3.49950045e-04 -4.95131128e-04 -9.74555267e-04\n",
       "   8.94299126e-04 -2.57504726e-04 -1.71152031e-04 -1.64264900e-04\n",
       "   1.35141949e-03  1.36086973e-03]]\n",
       "<NDArray 4x10 @cpu(0)>"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.initialize(force_reinit=True)\n",
    "\n",
    "x = nd.random.uniform(shape=(4, 1, 28, 28))\n",
    "y = net(x)\n",
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((6, 1, 5, 5), (84,))"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[0].weight.data().shape, net[5].bias.data().shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MixMLP(\n",
       "  (blk): Sequential(\n",
       "    (0): Dense(None -> 3, Activation(relu))\n",
       "    (1): Dense(None -> 4, Activation(relu))\n",
       "  )\n",
       "  (dense): Dense(None -> 5, linear)\n",
       ")"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class MixMLP(nn.Block):\n",
    "    def __init__(self, prefix=None, params=None):\n",
    "        super().__init__(prefix=prefix, params=params)\n",
    "        self.blk = nn.Sequential()\n",
    "        self.blk.add(\n",
    "            nn.Dense(3, activation='relu'),\n",
    "            nn.Dense(4, activation='relu')\n",
    "        )\n",
    "        self.dense = nn.Dense(5)\n",
    "\n",
    "    def forward(self, x):\n",
    "        y = nd.relu(self.blk(x))\n",
    "        print(y)\n",
    "        return self.dense(y)\n",
    "\n",
    "net = MixMLP()\n",
    "net"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "[[0.0000000e+00 0.0000000e+00 4.3653470e-04 1.0985183e-05]\n",
      " [1.2780711e-04 0.0000000e+00 2.5416636e-03 0.0000000e+00]]\n",
      "<NDArray 2x4 @cpu(0)>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "[[-8.7390354e-06 -2.9522511e-05  1.3910524e-05  2.8895573e-05\n",
       "   2.6100568e-05]\n",
       " [-5.0482504e-05 -1.6433572e-04  8.1579718e-05  1.6476015e-04\n",
       "   1.5433614e-04]]\n",
       "<NDArray 2x5 @cpu(0)>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.initialize(force_reinit=True)\n",
    "x = nd.random.uniform(shape=(2, 2))\n",
    "net(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method Parameter.data of Parameter dense8_weight (shape=(4, 3), dtype=float32)>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net.blk[1].weight.data"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "455583263cdbd810a2b2dc0736388df1171afe9106774a1560537fda80d7bd38"
  },
  "kernelspec": {
   "display_name": "Python 3.6.13 64-bit ('mxnet': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
